<?php

include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/User.php";

/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/20
 * Time: 10:10
 */
class UserDAO{
    const TABLE_USER = "user";
    const FIELD_ID = "userId";
    const FIELD_USERNAME = "username";
    const FIELD_PASSWORD = "password";
    const FIELD_PUBLISH_CREDIT = "creditPublish";
    const FIELD_WITHDRAW_CREDIT = "creditWithdraw";
    const FIELD_AVATAR = "headImg";
    const FIELD_TAG = "userTag";
    const FIELD_LOGIN_FLAG = "loginIndicator";
    const FIELD_WECHAT_ID = "weixinId";
    const FIELD_PHONE_NUM = "phone";

    public $conn;

    function __construct()
    {
        $this->conn = new db();
    }

    //插入用户
    public function insert($user){
        $tempUser = $this->queryByAccount($user[self::FIELD_USERNAME]);
        $tempUser2 = $this->queryByWechatId($user[self::FIELD_WECHAT_ID]);
        if($tempUser == null && $tempUser2 == null){
            $db = $this->conn->getDBConn();
            $stmt = $db->prepare("insert into user(username,password,creditPublish,creditWithdraw,headImg,userTag,loginIndicator,weixinId,phone) values(?,?,?,?,?,?,?,?,?)");
            $stmt->bindParam(1,mb_convert_encoding($user[self::FIELD_USERNAME],'GBK','UTF-8'),PDO::PARAM_STR);
            $stmt->bindParam(2,$user[self::FIELD_PASSWORD]);
            $stmt->bindParam(3,$user[self::FIELD_PUBLISH_CREDIT]);
            $stmt->bindParam(4,$user[self::FIELD_WITHDRAW_CREDIT]);
            $stmt->bindParam(5,mb_convert_encoding($user[self::FIELD_AVATAR],'GBK','UTF-8'),PDO::PARAM_STR);
            $stmt->bindParam(6,$user[self::FIELD_TAG]);
            $stmt->bindParam(7,$user[self::FIELD_LOGIN_FLAG]);
            $stmt->bindParam(8,$user[self::FIELD_WECHAT_ID]);
            $stmt->bindParam(9,$user[self::FIELD_PHONE_NUM]);
            $result = $stmt->execute();
            return $result;
        }
        else
            return -2;
    }

    //删除用户
    public function delete($userId){

    }

    //查询用户
    public function query($userId){
        $sql = "select * from user where ".self::FIELD_ID." = ".$userId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $user = $this->loadDataFromResult($row);
            return $user;
        }
        else
            return null;
    }

    //更新用户
    public function update($user){

    }

    //通过账号查询用户
    public function queryByAccount($account){
        $sql = "select * from user where ".self::FIELD_USERNAME." = '{$account}'";
		$db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $user = $this->loadDataFromResult($row);
            return $user;
        }
        else
            return null;
    }

    //通过微信号查询用户
    public function queryByWechatId($weixinId){
        $sql = "select * from user where ".self::FIELD_WECHAT_ID." = '{$weixinId}'";
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $user = $this->loadDataFromResult($row);
            return $user;
        }
        else
            return null;
    }

    //查询所有用户
    public function queryAllUser(){
        $sql = "select * from user";
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $users = array();
            foreach ($rows as $row){
                $user = $this->loadDataFromResult($row);
                array_push($users,$user);
            }
            return $users;
        }
        else
            return null;
    }

    //从结果集中构建出user对象
    private function loadDataFromResult($row){
        $user = new User();
        $user->setAccount($row[self::FIELD_USERNAME]);
        $user->setAvatar($row[self::FIELD_AVATAR]);
        $user->setId($row[self::FIELD_ID]);
        $user->setLoginFlag($row[self::FIELD_LOGIN_FLAG]);
        $user->setPassword($row[self::FIELD_PASSWORD]);
        $user->setPhoneNum($row[self::FIELD_PHONE_NUM]);
        $user->setPublishCredit($row[self::FIELD_PUBLISH_CREDIT]);
        $user->setTag($row[self::FIELD_TAG]);
        $user->setWeChatId($row[self::FIELD_WECHAT_ID]);
        $user->setWithdrawCredit($row[self::FIELD_WITHDRAW_CREDIT]);
        return $user;
    }
}
